package com.mo.task;

import com.mo.DlykApplication;
import com.mo.model.TActivity;
import com.mo.model.TDicType;
import com.mo.model.TDicValue;
import com.mo.model.TProduct;
import com.mo.result.DicEnum;
import com.mo.service.ActivityService;
import com.mo.service.DicTypeService;
import com.mo.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author mo
 * version
 */

@EnableScheduling
@Component
public class DataTask {
    @Resource
    private DicTypeService dicTypeService;
    @Resource
    private ProductService productService;
    @Resource
    private ActivityService activityService;

    //定时从数据库里读取所有字典类型和值的数据，把他放在Application这个全局hashMap的变量，相当于缓存在这个内存中,
    @Scheduled(fixedDelayString = "${project.task.delay}", zone = "Asia/Shanghai", timeUnit = TimeUnit.MILLISECONDS, initialDelay = 1000)
    public void task() {
        //查询字典数据
        List<TDicType> tDicTypeList = dicTypeService.loadAllData();
        tDicTypeList.forEach(tDicType -> {
            String typeCode = tDicType.getTypeCode();
            List<TDicValue> dicValueList = tDicType.getDicValueList();
            DlykApplication.cacheMap.put(typeCode, dicValueList);
        });
        //查询在售产品
        List<TProduct> tProductList = productService.getAllOnSaleProduct();
        DlykApplication.cacheMap.put(DicEnum.PRODUCT.getCode(), tProductList);

        List<TActivity> tActivityList = activityService.getOngoingActivity();
        DlykApplication.cacheMap.put(DicEnum.ACTIVITY.getCode(), tActivityList);
    }
}
